package com.banksoft.cps.controller.admin;

import com.banksoft.cps.bean.Company;
import com.banksoft.cps.bean.CompanyUser;
import com.banksoft.cps.bean.Device;
import com.banksoft.cps.bean.query.DeviceQuery;
import com.banksoft.cps.bean.query.PractitionerQuery;
import com.banksoft.cps.bean.warpper.DeviceWarpper;
import com.banksoft.cps.controller.ControllerConstants;
import com.banksoft.cps.service.CompanyService;
import com.banksoft.cps.service.DeviceService;
import com.banksoft.cps.util.PageUtils;
import com.banksoft.cps.util.UserContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by rongzhao.zhu on 2018/1/2.
 */
@Controller
@RequestMapping("/admin/device/")
public class AdminDeviceController extends ControllerConstants {
    @Autowired
    DeviceService deviceService;
    @Autowired
    CompanyService companyService;

    @RequestMapping(value = "list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list(DeviceQuery query){
        List<DeviceWarpper> list =  deviceService.listWarpper(query);
        return toJSONString(createDataTip(list));
    }
    @RequestMapping(value = "count", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String count(DeviceQuery query){
        long count =  deviceService.count(query);
        long pageCount = PageUtils.getPage(count,query.getSize());
        return toJSONString(createDataTip(pageCount));
    }
    @RequestMapping(value = "find",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String find(String id){
        Device device = deviceService.findById(id);
        DeviceWarpper warpper = new DeviceWarpper();
        if(device !=null){
            BeanUtils.copyProperties(device,warpper);

        }
        Company company = companyService.findById(device.getCompanyId());
        warpper.setCompany(company);
        return toJSONString(createDataTip(warpper));
    }

    @RequestMapping(value = "save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(Device bean, HttpServletRequest request){
        try {
            deviceService.save(bean);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }

    @RequestMapping(value = "listAll",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String listAll(HttpServletRequest request){
        DeviceQuery query = new DeviceQuery();
        List<DeviceWarpper> deviceList = deviceService.listWarpper(query);
        return toJSONString(createDataTip(deviceList));
    }

    @RequestMapping(value = "delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(String  id, HttpServletRequest request){
        try {
            deviceService.delete(id);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }

}
